<!--
 * @Descripttion: 文件说明
 * @version: 0.0.1
 * @Author: sunchangshun
 * @Date: 2021-02-18 07:58:07
 * @LastEditors: sunchangshun
 * @LastEditTime: 2021-04-13 16:41:21
-->
<template>
  <div class="messageList">
    <div id="chartJP" style="height: 100%;width: 100%"></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
import { Getmachine_plc_realdata } from "@/api";
export default {
  data() {
    return {
      saleList: null,
      MyChart: null,
      deviceId: '1588781350810136578',//设备编码
      dataTitle: [''],
      dataVal: [0],
      timerCount: 2000,
      timer:null
    };
  },
  mounted() {
    this.getData()
    this.timer = setInterval(() => {
      this.getData()
    }, this.timerCount);
  },
  methods: {
    getChart() {
      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          top: "15%",
          right: "3%",
          left: "5%",
          bottom: "25%",
        },
        xAxis: [
          {
            type: "category",
            data: this.dataTitle,
            axisLine: {
              lineStyle: {
                color: "rgba(255,255,255,0.12)",
              },
            },
            axisLabel: {
              interval:0,
              rotate:40,
              margin: 10,
              color: "#e2e9ff",
              textStyle: {
                fontSize: 14,
              },
            },
          },
        ],
        yAxis: [
          {
            axisLabel: {
              formatter: "{value}",
              color: "#e2e9ff",
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                color: "rgba(255,255,255,0.12)",
              },
            },
          },
        ],
        series: [
          {
            type: "bar",
            data: this.dataVal,
            barWidth: "20px",
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(0,244,255,1)", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "rgba(0,77,167,1)", // 100% 处的颜色
                    },
                  ],
                  false
                ),
                barBorderRadius: [30, 30, 30, 30],
                shadowColor: "rgba(0,160,221,1)",
                shadowBlur: 4,
              },
            },
            label: {
              normal: {
                show: true,
                lineHeight: 30,
                width: 80,
                height: 30,
                backgroundColor: "rgba(0,160,221,0.1)",
                borderRadius: 200,
                position: ["-8", "-60"],
                distance: 1,
                formatter: ["    {d|●}", " {a|{c}}     \n", "    {b|}"].join(","),
                rich: {
                  d: {
                    color: "#3CDDCF",
                  },
                  a: {
                    color: "#fff",
                    align: "center",
                  },
                  b: {
                    width: 1,
                    height: 30,
                    borderWidth: 1,
                    borderColor: "#234e6c",
                    align: "left",
                  },
                },
              },
            },
          },
        ],
      };
      if (this.myChart != null && this.myChart!=="" && this.myChart!== undefined) {
        this.myChart.setOption(option)
      }else{
        this.myChart = echarts.init(document.getElementById('chartJP'))
        this.myChart.setOption(option)
      }
    },
    getData(){
      Getmachine_plc_realdata(this.deviceId).then(res => {
        if (res.data.data!==null&&res.data.data!==undefined) {
          this.dataTitle =[]
          var datalist = res.data.data.filter(item=> item.key.includes('上一节拍'))
          for (let i = 0; i< datalist.length; i++) {
            this.dataTitle.push(datalist[i].key.replace(/上一节拍/g, ""))
            this.dataVal.push(datalist[i].value)
          }
          this.getChart()
        }
      }).catch(() => {
      })
    }
  },
  computed: {
  },
  destroyed() {
  // 清理定时器
  clearTimeout(this.timer)
  this.timer = null
}
};
</script>
<style lang="scss" scoped>
.messageList {
  height: 100%;
  width: 100%;
  .body-title {
    background: linear-gradient(90deg, #27316f 0%, rgba(13, 18, 42, 0.6) 100%);
  }
  .body-row {
    height: 40px;
    display: flex;
    align-items: center;
    padding-right: 60px;
    padding-left: 20px;
    margin: 3px 0;
    color: #a8adbf;
    font-size: 1.1rem;
    .value {
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      word-break: break-all;
      text-align: center;
    }
    .col1 {
      width: 80px;
    }
    .col2 {
      flex: 1;
      text-align: left !important;
    }
    .col3 {
      width: 260px;
    }
    .col4 {
      width: 230px;
    }
    &:nth-of-type(odd) {
      background: linear-gradient(
        90deg,
        rgba(13, 18, 42, 0.2) 0%,
        rgba(13, 18, 42, 0.2) 100%
      );
    }
  }
}
</style>
